//
//  SZLSellerViewController.m
//  Shengzhuli
//
//  Created by subook on 16/8/10.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLSellerViewController.h"
#import "SZLSellerMyBillViewController.h"
#import "SZLSellerGoodsManageViewController.h"
#import "SZLAccountHelper.h"
#import "SellerTableViewCell.h"
#import "BusinessNetwork.h"
#import "MBProgressHUD+MJ.h"
#import <UIButton+WebCache.h>
#import "SZLSingleListViewController.h"
#import "PersonSingleTypeTableViewCell.h"

@interface SZLSellerViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,weak) UIButton *iconButton;
@property (nonatomic,weak) UILabel *nameLabel;

@property (nonatomic,strong) NSArray *titleArray;
@property (nonatomic,strong) SellerInfoModel *model;

@end

@implementation SZLSellerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"我是商家";
    self.titleArray = @[@"销售明细",@"待发货",@"已发货",@"退货",@"商品管理"];
    [self requestShopInfo];
}
- (void)requestShopInfo
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"token"] = [SZLAccountHelper shareInstance].token;
    [self showWaiter];
    [BusinessNetwork getBusinessInfoWithParame:params response:^(BOOL success, NSString *msg, SellerInfoModel *model) {
        [self hideWaiter];
        if (success) {
            self.model = model;
            [self setTablview];
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (void)setTablview
{
    [self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titleArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SellerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SellerTableViewCell class]) forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.nameStr = self.titleArray[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:
        {
            SZLSellerMyBillViewController *svc = [[SZLSellerMyBillViewController alloc] init];
            [self.navigationController pushViewController:svc animated:YES];
        }
            break;
        case 1:case 2:case 3:
        {
            SZLSingleListViewController *singleListVc = [[SZLSingleListViewController alloc]init];
//            singleListVc.type = SingleTypeSellerUnSend+indexPath.row-1;
            [self.navigationController pushViewController:singleListVc animated:YES];
        }
            break;
        case 4:
        {
            SZLSellerGoodsManageViewController *szlv = [[SZLSellerGoodsManageViewController alloc] init];
            [self.navigationController pushViewController:szlv animated:YES];
        }
            break;
        default:
            break;
    }
}
- (UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, ScreenW, ScreenH/3))];

        UIImageView *bgView = [[UIImageView alloc] initWithFrame:_headerView.bounds];
        bgView.image = [UIImage imageNamed:@"bg_img_merchant"];
        bgView.userInteractionEnabled = YES;
        [_headerView addSubview:bgView];
        
        UIButton *iconButton = [[UIButton alloc] initWithFrame:(CGRectMake(ScreenW/2 - 40, 30, 80, 80))];
        self.iconButton = iconButton;
        iconButton.layer.cornerRadius = 40;
        iconButton.layer.borderWidth = 2;
        [iconButton.layer setMasksToBounds:YES];
        iconButton.layer.borderColor = [UIColor whiteColor].CGColor;
        [iconButton addTarget:self action:@selector(iconClick:) forControlEvents:(UIControlEventTouchUpInside)];
        [iconButton sd_setBackgroundImageWithURL:[NSURL URLWithString:self.model.portraitUrl] forState:(UIControlStateNormal) placeholderImage:GetImage(@"person_avatar")];
        [bgView addSubview:iconButton];

        UILabel *nameLabel = [[UILabel alloc] initWithFrame:(CGRectMake(ScreenW/2 - 100, 130, 200, 30))];
        self.nameLabel = nameLabel;
        nameLabel.textColor = [UIColor whiteColor];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.backgroundColor  = [UIColor clearColor];
        nameLabel.text = self.model.shopName;
        [bgView addSubview:_nameLabel];
    }
    return _headerView;
}
- (void)iconClick:(UIButton *)button
{
    
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, 0, ScreenW, ScreenH-64))];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = self.headerView;
        _tableView.showsHorizontalScrollIndicator = NO;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SellerTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([SellerTableViewCell class])];
        _tableView.backgroundColor = RGBColorOne(240);
        _tableView.tableFooterView = [[UIView alloc]init];
    }
    return _tableView;
}

@end
